package com.tuanzi.loan.web.bean.system;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tuanzi.loan.business.entity.system.Partner;
import com.tuanzi.loan.business.entity.system.RelationType;
import com.tuanzi.loan.business.entity.system.SystemUser;
import com.tuanzi.loan.business.service.system.PartnerService;
import com.tuanzi.loan.business.service.system.SystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.system.SystemUserSearchRequest;
import com.tuanzi.loan.vo.response.system.SystemUserSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.system.SystemUserLazyDataModel;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SystemUserListBean extends JSFListingBean<SystemUserSearchRequest, SystemUserSearchResponse> {

    private static final long serialVersionUID = 30992312030673477L;

    @Autowired
    private SystemUserService userService;
    @Autowired
    private PartnerService partnerService;

    private List<Partner> partners;

    @PostConstruct
    private void init() {
        initLazyDataModel();
        partners = partnerService.findAllEnable();
    }

    public void onReset() {
        initLazyDataModel();
    }

    public void onDisable(String id) {
        SystemUser user = userService.findOne(id);
        userService.disable(user);
        addSuccessfullyMessage();
    }

    public void onEnable(String id) {
        SystemUser user = userService.findOne(id);
        userService.enable(user);
        addSuccessfullyMessage();
    }

    public void onBatchDisable() {
        userService.disable(getSelectedUsers());
        addSuccessfullyMessage();
    }

    public void onBatchEnable() {
        userService.enable(getSelectedUsers());
        addSuccessfullyMessage();
    }

    private List<SystemUser> getSelectedUsers() {
        return userService.findAll(selectedRecords.parallelStream().map(SystemUserSearchResponse::getId)
                .map(x -> x.toString()).collect(Collectors.toList()));
    }

    public String onAdd() {
        return redirect("/views/system/user/user-info.xhtml");
    }

    public String onEdit(String id) {
        return redirect("/views/system/user/user-info.xhtml?id=" + id);
    }

    public String onRelatePermission(String id) {
        return redirect("/views/system/permission/functional/permission-relation?id=" + id + "&relationType="
                + RelationType.USER.name());
    }

    public String onRelateRole(String id) {
        return redirect("/views/system/user/user-role-relation?id=" + id);
    }

    public void onResetPassword(String id) {
        userService.resetPassword(id);
        addSuccessfullyMessage();
    }

    public void unlock(String id) {
        userService.unlock(id);
        addSuccessfullyMessage();
    }

    private void initLazyDataModel() {
        $ = new SystemUserSearchRequest();
        lazyDataModel = new SystemUserLazyDataModel($);
    }
}
